package com.leha.mp.core.Utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.InputStreamBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Base64;
import java.util.Date;
import java.util.ResourceBundle;

@Data
class Policy {
    Long deadline;
    Integer autoCompress = 1;
}

public class JFImgUtils {
    private static final String ak = ResourceBundle.getBundle("config").getString("jfimg.ak");
    private static final String sk = ResourceBundle.getBundle("config").getString("jfimg.sk");
    private static final String api_uri = ResourceBundle.getBundle("config").getString("jfimg.srv.api");
    public static String Token() {
        Policy policy = new Policy();
        policy.setDeadline(new Date().getTime()/1000+900);
        String JsonPolicy = JSON.toJSONString(policy);
        try {
            String encodeStr =
                Base64.getEncoder().encodeToString(JsonPolicy.getBytes("utf-8"));
            String akEncode = Base64.getEncoder().encodeToString(ak.getBytes("utf-8"));
            String hmac = HMacUtil.getSignature(encodeStr, sk, HMacUtil.HMAC_SHA256);
            String hmacEncode = Base64.getEncoder().encodeToString(hmac.getBytes("utf-8"));
            return akEncode+":"+encodeStr+":"+hmacEncode;
        } catch (Exception e)
        {
            return "";
        }

    }
    public static FileInfo uploadImgs(MultipartFile imgInput) throws IOException {
        //HashMap<String, String> map = new HashMap();
        //map.put("token", Token());
        //String ret = HttpUtils.postWithFormdata(api_uri, Token(),  new File("D:\\img\\q3.jpg"));
        CloseableHttpClient httpclient = HttpClients.createDefault();
        FileInfo info = new FileInfo();
        String url = null;
        try {
            HttpPost httppost = new HttpPost(api_uri);
            //String path="D:\\img\\q3.jpg";
            //File file = new File(path);
            //FileBody bin = new FileBody(file);
            InputStreamBody file = new InputStreamBody(imgInput.getInputStream(), imgInput.getOriginalFilename());
            StringBody token = new StringBody(Token(), ContentType.TEXT_PLAIN);
            System.out.println("Token: " + Token());
            HttpEntity reqEntity = MultipartEntityBuilder.create()
                    .addPart("file", file)
                    .addPart("token", token)
                    .build();
            httppost.setEntity(reqEntity);
            System.out.println("executing request " + httppost.getRequestLine());
            CloseableHttpResponse response = httpclient.execute(httppost);
            try {
                System.out.println("----------------------------------------");
                System.out.println(response.getStatusLine());
                HttpEntity resEntity = response.getEntity();
                String ret = EntityUtils.toString(resEntity);
                if (resEntity != null) {
                    System.out.println("Response content length: " + resEntity.getContentLength());
                    JSONObject jsonObject = JSON.parseObject(ret);
                    url = jsonObject.getString("url");
                }
                EntityUtils.consume(resEntity);
            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }
        info.setUrl(url);
        return info;
    }
}
